home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / TinyEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-08  |  3.8 KB  |  233 lines  |  [TEXT/KAHL]

  1. /*
  2.     TinyEdit - Minimal TransEdit Demonstration.
  3.  
  4.     The project should include TinyEdit.c (this file), TransEdit.c,
  5.     FakeAlert.c, TransSkel.c (or a project made from TransSkel.c)
  6.     and MacTraps.
  7.  
  8.     8 November 1986        Paul DuBois
  9. */
  10.  
  11. # include    <MenuMgr.h>
  12. # include    <FontMgr.h>
  13. # include    "TransEdit.h"
  14.  
  15.  
  16. # define    aboutAlrt    1000    /* "About..." alert number */
  17.  
  18.  
  19. typedef enum        /* File menu item numbers */
  20. {
  21.     new = 1,        /* begin new window */
  22.     open,            /* open existing file */
  23.     close,            /* close window */
  24.     /* --- */
  25.     quit = 5
  26. };
  27.  
  28.  
  29. typedef enum            /* Edit menu item numbers */
  30. {
  31.     undo = 1,
  32.     /* --- */
  33.     cut = 3,
  34.     copy,
  35.     paste,
  36.     clear
  37. };
  38.  
  39.  
  40. WindowPtr    lastFront = nil;    /* keeps track of front window */
  41. WindowPtr    editWind = nil;        /* non-nil if edit window open */
  42. MenuHandle    fileMenu;
  43. MenuHandle    editMenu;
  44.  
  45.  
  46.  
  47. /*
  48.     Set File/Edit menu items according to type of front window.
  49.  
  50.     The general behavior is:
  51.  
  52.     New and Open enabled if an edit window is not open, otherwise they
  53.     are disabled.
  54.  
  55.     Close enabled when an edit or DA window is in front (i.e.,
  56.     when there's a window at all).
  57.  
  58.     Undo disabled when the edit window is in front.
  59. */
  60.  
  61. SetMenus ()
  62. {
  63.     DisableItem (fileMenu, close);    /* assume no window at all */
  64.     EnableItem (editMenu, undo);
  65.  
  66.     if (FrontWindow () != nil)
  67.     {
  68.         EnableItem (fileMenu, close);
  69.         if (IsEWindow (FrontWindow ()))    /* the edit window's in front */
  70.             DisableItem (editMenu, undo);
  71.     }
  72.  
  73.     if (editWind == nil)
  74.     {
  75.         EnableItem (fileMenu, new);
  76.         EnableItem (fileMenu, open);
  77.     }
  78.     else
  79.     {
  80.         DisableItem (fileMenu, new);
  81.         DisableItem (fileMenu, open);
  82.     }
  83. }
  84.  
  85.  
  86. /*
  87.     Got an activate or deactivate.  It doesn't matter which, really.
  88.     Set the text menus appropriately for the front window, and draw
  89.     the menu bar, as these menus might change state from enabled to
  90.     disabled or vice-versa.
  91. */
  92.  
  93. Activate (active)
  94. Boolean    active;
  95. {
  96.     SetMenus ();
  97. }
  98.  
  99.  
  100. /*
  101.     Close selected from File menu, or close box of edit window was
  102.     clicked.
  103. */
  104.  
  105. Close ()
  106. {
  107.     if (EWindowClose (editWind))
  108.         editWind = nil;
  109.     SetMenus ();
  110. }
  111.  
  112.  
  113. /*
  114.     Make a new edit window.  Set the title to "Untitled" if not bound
  115.     to file, so that the window titling works the same whether
  116.     TransEdit is compiled in single or multiple window mode.
  117. */
  118.  
  119. MakeWind (bindToFile)
  120. Boolean    bindToFile;
  121. {
  122. Rect        r;
  123.  
  124.     SetRect (&r, 4, 45, 504, 335);
  125.     editWind = NewEWindow (&r, nil, false, -1L, true, 0L, bindToFile);
  126.     if (editWind != nil)
  127.     {
  128.         if (!bindToFile)
  129.             SetWTitle (editWind, "\pUntitled");
  130.         ShowWindow (editWind);
  131.     }
  132. }
  133.  
  134.  
  135. /*
  136.     File menu handler
  137. */
  138.  
  139. DoFileMenu (item)
  140. int        item;
  141. {
  142. WindowPtr    theWind;
  143.  
  144.     theWind = FrontWindow ();
  145.     switch (item)
  146.     {
  147.  
  148.     case new:
  149.         MakeWind (false);
  150.         break;
  151.  
  152.     case open:
  153.         MakeWind (true);
  154.         break;
  155.  
  156.     case close:
  157.         if (IsEWindow (theWind))
  158.             Close ();
  159.         else    /* DA in front */
  160.             CloseDeskAcc (((WindowPeek) theWind)->windowKind);
  161.         break;
  162.  
  163.     case quit:
  164.         if (ClobberEWindows () == true)
  165.             SkelWhoa ();
  166.         break;
  167.  
  168.     }
  169.     SetMenus ();
  170. }
  171.  
  172.  
  173. /*
  174.     Handle selection of About… item from Apple menu
  175. */
  176.  
  177. DoAbout ()
  178. {
  179.     (void) Alert (aboutAlrt, nil);
  180. }
  181.  
  182.  
  183. /*
  184.     Background procedure.  Check front window, reset menus if it
  185.     changes.
  186. */
  187.  
  188. CheckFront ()
  189. {
  190.     if (FrontWindow () != lastFront)
  191.     {
  192.         SetMenus ();
  193.         lastFront = FrontWindow ();
  194.     }
  195. }
  196.  
  197.  
  198. main ()
  199. {
  200.  
  201. /*
  202.     Initialize TransSkel, create menus and install handlers.
  203. */
  204.  
  205.     SkelInit ();
  206.  
  207.     SkelApple ("\pAbout TinyEdit…", DoAbout);
  208.  
  209.     fileMenu = NewMenu (1000, "\pFile");
  210.     AppendMenu (fileMenu, "\pNew/N;Open.../O;(Close/K;(-;Quit/Q");
  211.     SkelMenu (fileMenu, DoFileMenu, nil);
  212.  
  213.     editMenu = NewMenu (1001, "\pEdit");
  214.     AppendMenu (editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  215.     SkelMenu (editMenu, EWindowEditOp, nil);
  216.  
  217. /*
  218.     Do TransEdit-specific setup:  set creator for any files created,
  219.     set default event notification procedures for new windows.
  220. */
  221.  
  222.     SetEWindowProcs (nil, nil, Activate, Close);
  223.  
  224. /*
  225.     Process events until user quits,
  226.     then clean up and exit
  227. */
  228.  
  229.     SkelBackground (CheckFront);
  230.     SkelMain ();
  231.     SkelClobber ();
  232. }
  233.